package com.picket.controller;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletConfig;
import java.io.FileOutputStream;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.*;

import org.springframework.web.multipart.MultipartFile;

import com.picket.domain.FileUpload;

@Controller
@RequestMapping(value = "/uploadfile")
public class UploadFileController {
        private String uploadFolderPath;
        ServletConfig config;

        public String getUploadFolderPath() {
                return uploadFolderPath;
        }

        public void setUploadFolderPath(String uploadFolderPath) {
                this.uploadFolderPath = uploadFolderPath;
        }

        @RequestMapping(method = RequestMethod.GET)
        public String getUploadForm(Model model) {
                model.addAttribute(new FileUpload());
                return "/uploadfile";
        }

        @RequestMapping(method = RequestMethod.POST)
        public String create(FileUpload uploadItem, BindingResult result,
                        HttpServletRequest request, HttpServletResponse response,
                        HttpSession session) {
                if (result.hasErrors()) {
                        for (ObjectError error : result.getAllErrors()) {
                                System.err.println("Error: " + error.getCode() + " - "
                                                + error.getDefaultMessage());
                        }
                        return "/uploadfile";
                }

                // Some type of file processing...
                System.err.println("-------------------------------------------");
                try {
                        MultipartFile file = uploadItem.getFileData();
                        String fileName = null;
                        InputStream inputStream = null;
//                        OutputStream outputStream = null;
                        if (file.getSize() > 0) {
                                inputStream = file.getInputStream();
                                if (file.getSize() > 10000) {
                                        System.out.println("File Size:::" + file.getSize());
                                        fileName =  file.getOriginalFilename();
                        //outputStream = new FileOutputStream(fileName);
                                        System.out.println("fileName:" + file.getOriginalFilename());
                                        //InputStream inp = new FileInputStream(fileName);
                                        POIFSFileSystem fs = new POIFSFileSystem(inputStream);
                                        HSSFWorkbook wb = new HSSFWorkbook(fs);//.create(inputStream);
                                        Sheet sheet = wb.getSheetAt(0);
                                        Row row = sheet.getRow(2);
                                        Cell cell = row.getCell(1);
                                        System.out.println(cell.getStringCellValue());
                                        return "/uploadfile";
                                }
                                //InputStream inp = new FileInputStream("workbook.xls");
                                System.out.println("size::" + file.getSize());
                                fileName = request.getRealPath("") + "/images/"
                                                + file.getOriginalFilename();
                                //outputStream = new FileOutputStream(fileName);
                                System.out.println("fileName:" + file.getOriginalFilename());
/*
                                int readBytes = 0;
                                byte[] buffer = new byte[10000];
                                while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
                                        outputStream.write(buffer, 0, readBytes);
                                }*/
                               // outputStream.close();
                                inputStream.close();
                        }

                        // ..........................................
                        session.setAttribute("uploadFile", file.getOriginalFilename());
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return "redirect:/forms/uploadfileindex";
        }

}